//
//  DataTypeTranslate.m
//  UTDoorStyle
//
//  Created by chaiyanan on 14-9-28.
//  Copyright (c) 2014年 exmart. All rights reserved.
//

#import "DataTypeTranslate.h"
#import "Log.h"

@implementation DataTypeTranslate

#pragma mark -
#pragma mark - public Method

+ (NSString *)getAmOrPmWithDateStr:(NSString *)dateStr
{
    NSArray *dateAry = [dateStr componentsSeparatedByString:@":"];
    if ([dateAry[0] integerValue]>12)
    {
        return @"PM";
    }
    else
    {
        return @"AM";
    }
}

+ (NSString *)dateToString:(NSDate *)date withFormatterType:(NSString *)formatterType
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:formatterType];
    [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
    return [dateFormatter stringFromDate:date];
}

+ (NSDate *)string2Date:(NSString *)dateStr withFormatterType:(NSString *)formatterType
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:formatterType];
    [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    if (date)
    {
        
    }
    else
    {
        [dateFormatter setDateFormat:KDateFormatterTypeYMDHMS];
        date = [dateFormatter dateFromString:dateStr];
        if (!date)
        {
            [dateFormatter setDateFormat:KDateFormatterTypeYMDHM];
            date = [dateFormatter dateFromString:dateStr];
            
            if (!date)
            {
                [dateFormatter setDateFormat:KDateFormatterTypeYMD];
                date = [dateFormatter dateFromString:dateStr];
            }
        }
    }
    
    return date;
}

+ (NSString *)stringToString:(NSString *)str
{
    return [self NullToEmpty:str];
}

+ (id)stringToIntNumber:(NSString *)str
{
    if ([str isEqualToString:@""])
    {
        return @"";
    }
    return [NSNumber numberWithInt:[str intValue]];
}

+ (id)stringToDoubleNumber:(NSString *)str
{
    if ([str isEqualToString:@""])
    {
        return @"";
    }
    return [NSNumber numberWithDouble:[str doubleValue]];
    
}

+ (id)stringToFloatNumber:(NSString *)str
{
    if ([str isEqualToString:@""])
    {
        return @"";
    }
    return [NSNumber numberWithFloat:[str floatValue]];

}

+ (NSString *)numberToString:(NSNumber *)num
{
    if ([self isNumber:num])
    {
        return [NSString stringWithFormat:@"%@",num];
    }
    else
    {
        if ([self isNull:num])
        {
            return @"";
        }
        else
        {
            return (NSString *)num;
        }
    }

}

+ (NSString *)arrayToJsonString:(NSArray *)ary
{
    if (!ary)
    {
        ary = [[NSArray alloc]init];
    }
    NSString *jsonStr = [[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:ary options:kNilOptions error:nil] encoding:NSUTF8StringEncoding];
    return jsonStr;
}

+ (NSString *)dictionaryToJsonString:(NSDictionary *)dict
{
    return [[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil] encoding:NSUTF8StringEncoding];
}

+ (NSString *)encodeBase64String:(NSString *)base64Str
{
    LLog(@"base64Str:%@",base64Str);
    if (base64Str.length>0)
    {
        NSData *nsdata = [base64Str
                          dataUsingEncoding:NSUTF8StringEncoding];
        
        // Get NSString from NSData object in Base64
        NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0];
        
        LLog(@"base64Encoded:%@",base64Encoded);
        return base64Encoded;
    }
    else
    {
        return base64Str;
    }
    
}

+ (NSString *)decodeBase64String:(NSString *)base64Str
{
    LLog(@"base64Str:%@",base64Str);
    if (base64Str&&[base64Str isKindOfClass:[NSString class]]&&base64Str.length>0)
    {
        //        if (![self checkStrIsBase64Encoded:base64Str])
        //        {
        //            return base64Str;
        //        }
        NSData *dataFromBase64Str = [[NSData alloc]initWithBase64EncodedString:base64Str options:0];
        NSString *base64Decoded = [[NSString alloc]
                                   initWithData:dataFromBase64Str encoding:NSUTF8StringEncoding];
        LLog(@"base64Str:%@",base64Decoded);
        return base64Decoded;
    }
    else
    {
        return base64Str;
    }
}


#pragma mark -
#pragma mark - private Method


+ (NSString *)NullToEmpty:(NSString *)str
{
    if ([self isNull:str])
    {
        return @"";
    }
    else
    {
        return str;
        
    }
}

+ (BOOL)isNumber:(id)obj
{
    if ([obj isKindOfClass:[NSNumber class]])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+ (BOOL)isNull:(id)obj
{
    if ([obj isKindOfClass:[NSNull class]])
    {
        return YES;
    }
    else if(!obj)
    {
        return YES;
    }
    else if ([obj  isEqual: @"(null)"])
    {
        return YES;
    }
    else if ([obj isEqual:@"null"])
    {
        return YES;
    }
    else if ([obj isEqual:@"<null>"])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

@end
